package com.sunshine.lnuplus.ui.activity

import android.annotation.SuppressLint
import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.*
import android.view.LayoutInflater
import android.widget.TextView
import com.bumptech.glide.Glide
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.callback.LoginCallBack
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.service.LoadSchoolDateService
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.view.DialogWithView
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.activity_school_date.*

class SchoolDateActivity : BaseActivity() {

    private var conn: ServiceConnection? = null
    private lateinit var schoolDateList: ArrayList<ArrayList<String>>
    private lateinit var loading: DialogWithView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_school_date)

        title = "加载也是一种态度..."
        StatusBarUtils.setStatusBarWithColor(this, color)
        initView()
    }

    private fun initView() {
        val view = LayoutInflater.from(this).inflate(R.layout.dialog_loading, null, false)
        view.findViewById<TextView>(R.id.progress_text).text = "玩命加载中~"
        loading = DialogWithView.newInstance(view, "正在加载校历地址",false, null, null)
        loading.show(supportFragmentManager, "school_date")

        val intent = Intent(this, LoadSchoolDateService::class.java)
        var loadSchoolDateService: LoadSchoolDateService

        conn = object : ServiceConnection {
            override fun onServiceDisconnected(name: ComponentName) {}
            override fun onServiceConnected(
                name: ComponentName,
                service: IBinder
            ) {
                loadSchoolDateService = (service as LoadSchoolDateService.MyBind).getService()
                loadSchoolDateService.setCallBack(object : LoginCallBack {
                    override fun onSuccess() {
                        schoolDateList = loadSchoolDateService.getSchoolDateList()
                        InitData.functionList = schoolDateList
                        handler.sendEmptyMessage(1)
                    }

                    override fun onFail(status: String) {
                        handler.sendEmptyMessage(2)
                        Looper.prepare()
                        Toasty.warning(this@SchoolDateActivity, status).show()
                        Looper.loop()
                    }
                })
            }
        }
        bindService(intent, conn!!, Service.BIND_AUTO_CREATE)
    }

    @SuppressLint("HandlerLeak")
    val handler = object : Handler(){
        @SuppressLint("CheckResult")
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when(msg.what){
                1 ->{
                    loading.dismiss()
                    if(schoolDateList.size > 0){
                        title = schoolDateList[0][0]
                        Glide
                            .with(this@SchoolDateActivity)
                            .load(schoolDateList[0][1])
                            .fitCenter()
                            .placeholder(R.mipmap.loading)
                            .into(school_date_image)
                    } else {
                        Toasty.error(this@SchoolDateActivity, "哎呀，找不到地址了").show()
                    }
                }
                2 -> {
                    loading.dismiss()
                    title = "校历"
                }

            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if(conn != null){
            unbindService(conn!!)
        }
    }
}
